import { memo, useEffect, forwardRef, useImperativeHandle, useState,useRef } from 'react'
import { Select } from 'antd'
import bus from '../../utils/eventBus'
const LdCloner = forwardRef((props: any, ref: any) => {
    const { value = '', getSelect } = props
    const [selfValue, setSelValue] = useState<string>('')
    const [options, setOption] = useState<any[]>([])
    useEffect(() => {
        setSelValue(value)
    }, [value])
    //返回刻录机的名称以及id
    const getBurnInfo = () => {
        const params = {
            burnerId: selfValue.toString(),
            burnerName: ''
        }
        options.map(item => {
            if (item.burnerId === selfValue) {
                params.burnerName = item.burnerName
            }
        })
        return params
    }
    const getDefaultBurnerFinished = (res: any) => {
        console.log('默认刻录机', res)
        if (res.code === 200) {
            setSelValue(res.defaultBurner)
            getSelect && getSelect(res.defaultBurner)
        }
    }
    const load = () => {
        console.log("获取刻录机数据", "window.chromePro.getBurnerList");
        window.chromePro.getBurnerList();
    }
    /**
     * 本地刻录机
    */
    const getBurnerListFinished = (res: any) => {
        console.log(res, "本地刻录机数据");
        if (res.code === 200) {
            setOption(origin => {
                const data = [...origin, ...res.data]
                data.map(item => {
                    item.label = item.burnerName
                    item.value = item.burnerId
                })
                return [...data]
            })
        }
    }
    /**
     * 网络刻录机
    */
    const getNetBurnerListFinished = (res: any) => {
        console.log(res, '网络刻录机数据')
        if (res.code === 200) {
            setOption(origin => {
                const data = [...origin, ...res.data]
                data.map(item => {
                    item.label = item.burnerName
                    item.value = item.burnerId
                })
                return [...data]
            })
            window.chromePro.getDefaultBurner();
        }
    }
    const handleChange = (value: string) => {
        getSelect && getSelect(value)
    }
    useEffect(() => {
        bus.addListener("getBurnerListFinished", getBurnerListFinished);
        bus.addListener("getNetBurnerListFinished", getNetBurnerListFinished);
        bus.addListener('getDefaultBurnerFinished', getDefaultBurnerFinished)
        window.chromePro.getNetBurnerList()
        if (window.chromePro) {
            window.chromePro.getBurnerList();
        } else {
            bus.addListener("load", load);
        }
    }, [])
    useEffect(() => {
        return () => {
            bus.removeListener("getBurnerListFinished", getBurnerListFinished);
            bus.removeListener("load", load);
            bus.removeListener("getNetBurnerListFinished", getNetBurnerListFinished);
            bus.removeListener('getDefaultBurnerFinished', getDefaultBurnerFinished)
        }
    }, [])
    useImperativeHandle(ref, () => ({
        getBurnInfo
    }))
    return (
        <>
            <Select value={selfValue} style={{ width: '100%' }} options={options} onChange={handleChange} placeholder="请选择刻录机"></Select>
        </>
    )
})
export default memo(LdCloner)